﻿//Copyright (c) 2010-2011, Andreas Grünwald
//All rights reserved.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows;
using yaTDL.Properties;
using yaTDL.Core.MVVM;

namespace yaTDL
{
    public class ColorConverterBase
    {
        protected static Brush red = (Brush)Application.Current.Resources["TaskList_Red"];
        protected static Brush yellow = (Brush)Application.Current.Resources["TaskList_Yellow"];
        protected static Brush green = (Brush)Application.Current.Resources["TaskList_Green"];
        protected static Brush blue = (Brush)Application.Current.Resources["TaskList_Blue"];
        protected static Brush transparent = new SolidColorBrush(Color.FromArgb(0, 1, 1, 1));

        protected Brush getForeground(Brush background)
        {
            if (background == red || background == blue)
            {
                return Brushes.White;
            }
            else
            {
                return Brushes.Black;
            }     
        }
    }


    public class DateTimeColorConverter:ColorConverterBase, IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object _parameter, System.Globalization.CultureInfo culture)
        {
            string parameter = (string)_parameter;

            switch (parameter)
            {
                case "Background":
                    if ((bool)values[1])
                    {
                        return transparent;
                    }
                    else
                    {
                        if (((DateTime)values[0]).Date == DateTime.Parse("01.01.0001").Date)
                        {
                            return transparent;
                        }

                        TimeSpan difference = ((DateTime)values[0]).Date - DateTime.Now.Date;

                        if (difference.TotalDays == 0)
                        {
                            return green;
                        }
                        if (difference.TotalDays < 0)
                        {
                            return red;
                        }

                        if (difference.TotalDays > 0)
                        {
                            return yellow;
                        }

                        return transparent;
                    }                    

                case "Foreground":
                    return getForeground((Brush)Convert(values, targetType, "Background", culture));                                                

                case "BorderBrush":
                    if(UISettings.Default.Tasklist_ShowBordersForInHistory)
                        return ((DateTime)values[0]).Date == DateTime.Parse("01.01.0001").Date ? Brushes.Transparent : Brushes.Black;
                    else
                        return ((DateTime)values[0]).Date == DateTime.Parse("01.01.0001").Date || (bool)values[1] ? Brushes.Transparent : Brushes.Black;

                default:
                    return null;                    
            }
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
        
    }


    public class PriorityColorConverter:ColorConverterBase, IMultiValueConverter
    {


        public object Convert(object[] values, Type targetType, object _parameter, System.Globalization.CultureInfo culture)
        {
            string parameter = (string)_parameter;

            switch (parameter)
            {
                case "Foreground":
                    return getForeground((Brush)Convert(values, targetType, "Background", culture));
                case "Background":
                    if ((bool)values[1])
                    {
                        return transparent;
                    }
                    else
                    {
                        switch ((int)values[0])
                        {
                            case -1:
                                return transparent;
                            case 0:
                                return yellow;
                            case 1:
                                return green;
                            case 2:
                                return blue;
                            case 3:
                                return red;
                            default:
                                return transparent;
                        }
                    }     
                case "BorderBrush":
                    if (UISettings.Default.Tasklist_ShowBordersForInHistory)
                        return Brushes.Black;
                    else
                        return (bool)values[1] ? transparent : Brushes.Black;
                    
                default:
                    return null;                    
            }
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
        
    }


    public class InHistoryValueConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (targetType == typeof(FontStyle))
            {
                return (bool)value ? FontStyles.Italic : FontStyles.Normal;
            }

            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }


    public class SortByToStringConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            switch ((SortBy) value)
	        {
                case SortBy.LastEdited:
                    return "LAST EDIT";
                default:
                    return ((SortBy)value).ToString().ToUpper();
	        }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }


    public class DoubleAdder : IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value is double)
            {
                return (double)value + double.Parse((String)parameter);
            }
            else
            {
                return value;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value is double)
            {
                return (double)value + 24;
            }
            else
            {
                return value;
            }
        }
    }


    public class DoubleCalculator : IMultiValueConverter
    {
        static char[] supportedOperators = new char[] { '+', '-' };

        public object Convert(object[] values, Type targetType, object _parameter, System.Globalization.CultureInfo culture)
        {
            try
            {
                string parameter = (string)_parameter;

                List<int> indizes = new List<int>();
                string operatorString = "";
               
                foreach (string str in parameter.Split(supportedOperators, StringSplitOptions.RemoveEmptyEntries))
                {
                    indizes.Add(Int32.Parse(str));
                }

                foreach (char chr in parameter.ToCharArray())
                {
                    if (supportedOperators.Contains(chr)) operatorString += chr;
                }


                double result = (double)values[indizes[0]];

                for (int i = 0; i < operatorString.Length; i++)
                {
                    switch (operatorString.Substring(i,1))
                    {
                        case "+":
                            result += (double)values[indizes[i+1]];
                            break;

                        case "-":
                            result -= (double)values[indizes[i+1]];
                            break;
                    }
                }

                return result;

            }
            catch (Exception)
            {
                return 0;
            }
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class VisibilityDoubleConverter : IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return (Visibility)value == Visibility.Visible ? double.Parse((string)parameter) : 0;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return (double)value == 0 ? Visibility.Hidden : Visibility.Visible;
        }
    }

    public class BoolDoubleConverter : IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return (bool)value ? double.Parse((string)parameter) : 0;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return (double)value == 0 ? Visibility.Hidden : Visibility.Visible;
        }
    }


}
